using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TTSoft.MyWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TTSoft.MyWeb.Models.ViewModels;
	using TTSoft.MyWeb.Models;
	using TTSoft.MyWeb.Models.Repositories;
	using TTSoft.MyWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectThuoc()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THUOC
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectThuocPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THUOC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListThuoc", CreateViewModel(data));
		}

		public ActionResult SelectThuocByIDLoaiThuoc(int idloaithuoc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiThuoc = idloaithuoc,
				EnumViewModelType = EnumViewModel.ADMIN_THUOC,
				CurrentPage = page
			};
			return View("SelectThuoc", CreateViewModel(data));
		}

		public ActionResult SelectThuocByIDLoaiThuocPaging(int idloaithuoc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiThuoc = idloaithuoc,
				EnumViewModelType = EnumViewModel.ADMIN_THUOC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListThuoc", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteThuoc(int id)
		{
			var deleteItem = _repThuoc.SelectByID(id);
			var fileName = deleteItem.HinhAnh;
			var idloaithuoc = deleteItem.IDLoaiThuoc;
			var result = _repThuoc.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repThuoc.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfThuoc(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THUOC,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateThuoc(FormCollection forms)
		{
			string id = forms.GetValues("Thuoc_ID")[0];
			string idloaithuoc = forms.GetValues("Thuoc_IDLoaiThuoc")[0];
			string tenthuoc = forms.GetValues("Thuoc_TenThuoc")[0];
			string hamluong = forms.GetValues("Thuoc_HamLuong")[0];
			string donvitinh = forms.GetValues("Thuoc_DonViTinh")[0];
			string nuocsanxuat = forms.GetValues("Thuoc_NuocSanXuat")[0];
			string handung = forms.GetValues("Thuoc_HanDung")[0];
			string ngaysanxuat = forms.GetValues("Thuoc_NgaySanXuat")[0];
			string soluong = forms.GetValues("Thuoc_SoLuong")[0];
			string dongia = forms.GetValues("Thuoc_DonGia")[0];
			string bhyt = forms.GetValues("Thuoc_BHYT")[0];
			var file = Request.Files["Thuoc_HinhAnh"];
			string refName = "";
			var editItem = _repThuoc.SelectByID(int.Parse(id));
			editItem.IDLoaiThuoc = int.Parse(idloaithuoc.Replace(".", "").Replace(",", ""));
			editItem.TenThuoc = tenthuoc;
			editItem.HamLuong = hamluong;
			editItem.DonViTinh = donvitinh;
			editItem.NuocSanXuat = nuocsanxuat;
			editItem.HanDung = int.Parse(handung.Replace(".", "").Replace(",", ""));
			editItem.NgaySanXuat = ngaysanxuat == "" ? DateTime.Now :DateTime.Parse(ngaysanxuat);
			editItem.SoLuong = int.Parse(soluong.Replace(".", "").Replace(",", ""));
			editItem.DonGia = long.Parse(dongia.Replace(".", "").Replace(",", ""));
			editItem.BHYT = bhyt;
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.HinhAnh = refName;
			}
			result = _repThuoc.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THUOC,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfThuoc", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertThuoc(FormCollection forms)
		{
			string idloaithuoc = forms.GetValues("Thuoc_IDLoaiThuoc")[0];
			string tenthuoc = forms.GetValues("Thuoc_TenThuoc")[0];
			string hamluong = forms.GetValues("Thuoc_HamLuong")[0];
			string donvitinh = forms.GetValues("Thuoc_DonViTinh")[0];
			string nuocsanxuat = forms.GetValues("Thuoc_NuocSanXuat")[0];
			string handung = forms.GetValues("Thuoc_HanDung")[0];
			string ngaysanxuat = forms.GetValues("Thuoc_NgaySanXuat")[0];
			string soluong = forms.GetValues("Thuoc_SoLuong")[0];
			string dongia = forms.GetValues("Thuoc_DonGia")[0];
			string bhyt = forms.GetValues("Thuoc_BHYT")[0];
			var file = Request.Files["Thuoc_HinhAnh"];
			string refName = "";
			var newItem = new Thuoc
			{
				IDLoaiThuoc = int.Parse(idloaithuoc),
				TenThuoc = tenthuoc,
				HamLuong = hamluong,
				DonViTinh = donvitinh,
				NuocSanXuat = nuocsanxuat,
				HanDung = int.Parse(handung),
				NgaySanXuat = ngaysanxuat == "" ? DateTime.Now : DateTime.Parse(ngaysanxuat),
				SoLuong = int.Parse(soluong),
				DonGia = long.Parse(dongia),
				BHYT = bhyt,
				HinhAnh = "noimages.jpg"
			};
			var result = _repThuoc.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repThuoc.SelectByID(newItem.ID);
						addedItem.HinhAnh = refName;
						_repThuoc.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDLoaiThuoc = int.Parse(idloaithuoc),
				EnumViewModelType = EnumViewModel.ADMIN_THUOC,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectThuoc", CreateViewModel(data));
		}

        public ActionResult EnterTimKiemThuoc()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ENTER_SEARCH_THUOC
            };
            return View("TimKiem", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimKiemThuoc(FormCollection forms)
        {
            string tenthuoc = forms.GetValues("Thuoc_TenThuoc")[0];
            string nuocsanxuat = forms.GetValues("Thuoc_NuocSanXuat")[0];

            var data = new DataTransferViewModel
            {
                Search_TenThuoc = tenthuoc,
                Search_NuocSanXuat = nuocsanxuat,
                EnumViewModelType = EnumViewModel.ADMIN_SEARCH_THUOC
            };
            return View("TimKiem", CreateViewModel(data));
        }
	}
}